package com.example.controller;

import com.example.common.Result;
import com.example.entity.StudyMaterial;
import com.example.service.StudyMaterialService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/studyMaterial")
public class StudyMaterialController {
    @Resource
    private StudyMaterialService studyMaterialService;

    @PostMapping("/add")
    public Result add(@RequestBody StudyMaterial studyMaterial) {
        return studyMaterialService.add(studyMaterial);
    }

    @GetMapping("/page")
    public Result page(StudyMaterial studyMaterial,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) Boolean isAdmin) {
        PageInfo<StudyMaterial> page = studyMaterialService.page(studyMaterial, pageNum, pageSize, isAdmin);
        return Result.success(page);
    }


    @PostMapping("/changeStatus/{id}")
    public Result changeStatus(@PathVariable Long id, @RequestParam Integer status) {
        return studyMaterialService.changeStatus(id, status);
    }

    @PostMapping("/incrementDownload/{id}")
    public Result incrementDownload(@PathVariable Long id) {
        return studyMaterialService.incrementDownload(id);
    }
}